// File Name: rs485_task.c
// Author: kukukukuku~
// Date: 2024-12-21

#include "./Key/inc/key_var.h"
#include "./OLED/inc/oled_var.h"
#include "./RS485/inc/rs485_task.h"
#include "./USART/inc/usart_var.h"

//RS485任务句柄
TaskHandle_t rs485TaskHandle;


/**
 * @brief   RS485任务
 * @param   arg:    任务参数
 * @retval  None
 * @note    None
*/
void rs485_task(void *arg)
{
    rs485_object_init(&rs485Obj, rs485TxBuffer, rs485RxBuffer);
    
    while(1)
    {
        //RS485接收到数据，转发到USART
        if(rs485_rx_finish_flag_get(&rs485Obj) == RS485_RX_STATE_COMPLETE)
        {
            rs485_received_data_handle(&rs485Obj);
            usart_send_data(&usartObj, rs485_rx_buffer_get(&rs485Obj), rs485_rx_data_len_get(&rs485Obj));
            ++rs485ToUsartCnt;
            
            rs485_rx_buffer_clear(&rs485Obj);
            rs485_rx_data_len_clear(&rs485Obj);
            rs485_rx_finish_flag_clear(&rs485Obj);
            
            oledRefreshFlag = 1;
        }
        
        //清除RS485转发到USART的计数
        if(key_is_click_triged(&keyClearObj))
        {
            rs485ToUsartCnt = 0;
            usartToRs485Cnt = 0;
            oledRefreshFlag = 1;
            key_clear_click_handle(&keyClearObj);
        }
        
        vTaskDelay(5);
    }
}
